package cn.yunhe.validator;


import cn.yunhe.entity.Role;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class RoleValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        //判断验证是否为Role实体类,如果是,就验证
        return Role.class.equals(aClass);
    }

    @Override
    public void validate(Object o, Errors errors) {
        Role role = (Role) o;
        double dis = role.getTotal() - (role.getPrice() * role.getCount());
        if (Math.abs(dis) > 0.01) {
            errors.rejectValue("total", null, "交易金额与购买数量不匹配");
        }
    }
}
